package com.mdd.front.controller.yangyangLeadingEnterprise.personalCenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.mdd.common.aop.NotLogin;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppOrder;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppOrderItem;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.CommonParam;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.order.OrderSelectParam;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppOrderItemService;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/order")
@Api(tags = "财税报告管理")
public class AppOrderController {

    @Resource
    private ITAppOrderService appOrderService;

    @Resource
    private ITAppOrderItemService appOrderItemService;

//    @NotLogin
//    @PostMapping("/selectOrderPayTypeList")
//    @ApiOperation(value="获取支付方式")
//    @ResponseBody
//    public String selectOrderPayTypeList(HttpServletRequest request) {
//        return JsonReturn.success().setListData(Arrays.asList("微信支付", "支付宝支付")).toJsonStr();
//    }

    @NotLogin
    @PostMapping("/startPayment")
    @ApiOperation(value="支付生成报告/发起支付")
    @ResponseBody
    public String startPayment(@RequestBody CommonParam param, HttpServletRequest request) {
        String jsonStr = param.getJsonStr();
        if (jsonStr != null && !jsonStr.isEmpty()) {
            JSONObject jsonObject = JSON.parseObject(jsonStr);
            JSONObject map = jsonObject.getJSONObject("map");
            JSONObject order = map.getJSONObject("order");
            JSONArray orderItemList = map.getJSONArray("orderItemList");
            TAppOrder orderData = new Gson().fromJson(order.toString(), TAppOrder.class);
            appOrderService.insert(orderData);

            for (int i = 0; i < orderItemList.size(); i++) {
                TAppOrderItem item = new Gson().fromJson(orderItemList.getJSONObject(i).toString(), TAppOrderItem.class);
                item.setOrderId(orderData.getId());
                appOrderItemService.insert(item);
            }
        //todo  调起微信支付

        }

        return JsonReturn.success().toJsonStr();
    }

    @NotLogin
    @PostMapping("/selectOrderList")
    @ApiOperation(value="订单列表")
    @ResponseBody
    public String selectOrderList(@RequestBody OrderSelectParam param, HttpServletRequest request) {
        return appOrderService.selectOrderList(param);
    }

    @NotLogin
    @PostMapping("/viewReport")
    @ApiOperation(value="查看/下载报告")
    @ResponseBody
    public String viewReport(@RequestBody CommonParam param, HttpServletRequest request) {
        Integer orderId = param.getId();
        List<TAppOrderItem> orderItemList =appOrderItemService.selectOrderItemListByOrderId(orderId);
        return JsonReturn.success().setListData(orderItemList).toJsonStr();
    }

}
